<?php

namespace Admin\Frontend\Controllers;

use Phalcon\Mvc\View;
use Helpers\Tool;
use Helpers\Main;
use Helpers\Cache;
use Helpers\Assets;
use Helpers\Template;
use Helpers\EnumList;
use Helpers\HttpClient;
use Helpers\UserPower;
use Admin\Models\schedule\XhusersSchedule;


class ScheduleController extends ControllerBase
{

    /**
     * 学校-学生列表页面
     *
     * create by ck 20160318
     * modify by ck 20160621
     */
    public function userScheduleAction()
    {
        $this->view->pick('schedule/userSchedule');
        Assets::easyUI('js/schedule/userSchedule.js');
        Assets::importMainJs('assets/xh/xhForm.js');
        Assets::FullCalendar();
        Main::showNavPosition();
        //UserPower::checkUserPower('Schedule_userSchedule') or die(Main::showMessageModal('对不起，您没有查看此页面的权限！', 109106007));
        Template::exists('Schedule_User', 'list') && Template::exists('XHSys_User', 'list')
        or die(Main::showMessageModal('配置文件不存在，请先创建！', 109106008));
        $schSearchArr[] = array('type'=>'text', 'id'=>'SchoolList_Search', 'label'=>'搜索： ');
        $schSearchToolbar = Template::createSearchToolBar($schSearchArr);
        $this->view->setVars(array(
            'ScheduleUser'    => Template::createList('ScheduleUser', 'Schedule_User',array(
                'searchToolbar' => $schSearchToolbar,
                'checkbox'      => false,
                'field'         => array('iUserId', 'sStaffName','sPostName'),
                'tableAttr'     => array(
                    'singleSelect'  => 'true'
                )
            )),
            'ScheduleList'   => Template::createList('ScheduleList','Schedule_List', array(
                'checkbox'      => false,
                'field'         => array('iUserId','sTitle','tStartTime', 'tEndTime', 'iScheduleType', 'tCreateTime'),
                'tableAttr'     => array(
                    'singleSelect'  => 'true'
                )
            ))
        ));
    }

    public function getUserDataAction(){
        $this->view->disable();
//        $this->request->isPost() or die;
//        $support = $this->request->getPost('bSupport', 'int');
//        $status = $this->request->getPost('status', 'int');
        $parameters = array(
            'columns'    => 'iStaffId,iUserId,sStaffName,sPostName',
            'conditions' => 'iUserId > 0 and iStaffStatus = 1'
        );
//        if($support){
//            $parameters['conditions'] .= ' AND (iStatus = 1 OR iStatus = 2)';
//        }
//        if($status != ''){
//            $parameters['conditions'] .= ' AND iStatus = '. $status;
//        }
        Main::runSearchVagueConditions($parameters['conditions'], ['iUserId', 'sStaffName']);
        return $this->response->setJsonContent(Main::runModelPage('\\Admin\\Models\\hrm\\XhhrmStaff', $parameters, $this->schEnum));
    }

    /**
     * #方法说明：日程列表获取
     * @return mixed
    # Created by zsc
     */
    public function getListAction(){
        $this->view->disable();
        $this->request->isPost() or die;
        $parameters = array(
            'columns'    => 'iScheduleId,sTitle,iUserId,tStartTime,tEndTime,iScheduleType,tCreateTime',
            'conditions'    => 'iStatus in( {statusArr:array} ) AND iUserId = {iUserId} AND iScheduleType = {iScheduleType} ',
            'bind' => array('statusArr'=>array(1) ,'iScheduleType' => $this->request->getPost('iScheduleType'),'iUserId' =>$this->request->getPost('iUserId')  ),
        );
        $result = XhusersSchedule::find($parameters)->toArray();
        $data = array();
        if($result)foreach ($result as $key => $value){
            $color = '#ff9f89';
            if($value['tStartTime'] <= date('Y-m-d') && $value['tEndTime'] >= date('Y-m-d')){
                $color = '#ff9f89';
            }elseif($value['tEndTime'] < date('Y-m-d')){
                $color = '#ff9900';
            }elseif($value['tStartTime'] > date('Y-m-d')){
                $color = '#336600';
            }
            $data[] = array(
                'id' => $value['iScheduleId'],
                'title' => $value['sTitle'],
                'start' => $value['tStartTime'],
                'end' => $value['tEndTime'],
                //'end' => '2017-12-22',
                'color' => $color,
            );
        }
        return $this->response->setJsonContent($data);
    }



    /**
     * #方法说明：日程添加
     * # Created by zsc
     */
    public function addScheduleAction(){
        Assets::importMainJs('assets/laydate/laydate.js');
        Assets::easyUI('js/schedule/addSchedule.js');
        $sActionName = 'add';
        if($this->request->get('iScheduleId', 'int')){
            $model = XhusersSchedule::findFirst([
                'conditions' => "iScheduleId=:iScheduleId:",
                'bind' => array('iScheduleId' => $this->request->get('iScheduleId')),
            ]);
            $sActionName = 'edit';
            $model or die('未找到相应的日程');
        }
        $model or $model = new XhusersSchedule();
        $sActionName == 'add' && $model->tStartTime = date('Y-m-d',$this->request->get('startTime'));
        if( $this->request->isPost()){
            $this->view->disable();
            //if(!$sPeriodTime[0] && !$sPeriodTime[1])die('请选择正确的起始时间');
            // if($sPeriodTime[0] > $sPeriodTime[1])die('开始时间不能大于结束时间');
            $model->sTitle = $this->request->getPost('sTitle');
            $model->sIntro = $this->request->getPost('sIntro');
            $model->tStartTime =  $this->request->getPost('tStartTime');
            $model->tEndTime = $this->request->getPost('tEndTime')?$this->request->getPost('tEndTime'):$this->request->getPost('tStartTime');
            $model->iStatus = $this->request->getPost('iStatus')?$this->request->getPost('iStatus'):1;
            if($sActionName == 'add'){
                $this->request->get('iUserId') or die('无法确定添加的用户');
                $model->iUserId = $this->request->get('iUserId');
                $model->tCreateTime = date('Y-m-d H:i:s');
                $model->iCreateUid = Main::getLoginUserInfo('userId');
                $model->iScheduleType = $this->request->get('iScheduleType');
            }
            $result = $model->save();
            die($result?$result:'保存失败');
        }
        $this->view->setVar('model',$model);
        $this->view->pick('schedule/addSchedule');
    }

    /**
     * #方法说明：日程查看
     * # Created by zsc
     */
    public function viewScheduleAction(){


        $this->view->pick('schedule/viewSchedule');
    }



}

